package com.xzs.jersey.resource;

import com.xzs.jersey.bean.LoginVO;
import com.xzs.jersey.bean.ResultVO;
import org.springframework.stereotype.Component;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;

@Component
@Path("/index")
public class StudentResource {

    @POST
    @Produces({MediaType.APPLICATION_JSON})
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/student")
    public ResultVO getStudent(@Context HttpServletRequest request, LoginVO loginVO) {

        System.out.println(loginVO.toString());


        ResultVO resultVO = new ResultVO();
        resultVO.setMessage("登录成功");
        resultVO.setSuccess("true");
        return resultVO;
    }

    /**
     * 二维码生成工具
     *
     * @throws Exception
     */
    @GET
    @Path("/getWXQRCode")
    public void getWXQRCode(@Context HttpServletRequest request,
                            @Context HttpServletResponse response) throws Exception {

        String token = "24_Tu79ARiosJCvKNGkjfywGCb0Ea6CBGXLAVbCXkK1SPJCYa_U51fT_c1wSqLJUB6lZBICDmeKoq57KP0XqzF-MbjhOgaaXOyqfCaCbj4y3nUasrNju9Kqgo3P9CYukV-GTyEQdZbxIgt35HuKBAJfAFAUTC";
        String qrCodeUrl = "https://api.weixin.qq.com/wxa/getwxacode?access_token=${ACCESSTOKEN}";
        qrCodeUrl = qrCodeUrl.replace("${ACCESSTOKEN}", token);
        String param = "{\"path\":\"pages/index/index?agencyCode=997d53bf01774783a95ed832cfe7d422\",\"width\":430}";

        URL url = new URL(qrCodeUrl);
        HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
//        httpUrlConn.setSSLSocketFactory(ssf);

        httpUrlConn.setDoOutput(true);
        httpUrlConn.setDoInput(true);
        httpUrlConn.setUseCaches(false);
        // 设置请求方式（GET/POST）
        httpUrlConn.setRequestMethod("POST");
        // 当有数据需要提交时
        if (null != param) {
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            outputStream.write(param.getBytes("UTF-8"));
            outputStream.close();
        }

        // 将返回的输入流转换成字符串
        InputStream inputStream = httpUrlConn.getInputStream();

        /* 设置文件ContentType类型，这样设置，会自动判断下载文件类型 */
        response.setContentType("multipart/form-data");
        /* 设置文件头：最后一个参数是设置下载文件名 *///+financeInvoiceDTO.getGraphicType()
        String fileName = URLEncoder.encode("qrCode.png", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            byte[] b = new byte[1024];
            int nRead = 0;
            while ((nRead = inputStream.read(b)) != -1) {
                os.write(b, 0, nRead);
            }
            // 释放资源
            inputStream.close();
            os.flush();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}